package com.groupbuying.customer.utils;

import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**
 * @anthor wenguoqiang
 * @date: 2019/01/11 15:40
 * @company 蒙泰互联网公司
 * @des
 **/
public class ChineseCharacters2PinyinUtils {

    public static String toHanyuPinyin(String ChineseLanguage)
    {
        char[] cl_chars = ChineseLanguage.trim().toCharArray();
        String hanyupinyin = "";
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        defaultFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
        try {
            for (int i = 0; i < cl_chars.length; i++)
                if (String.valueOf(cl_chars[i]).matches("[一-龥]+"))
                    hanyupinyin = hanyupinyin + net.sourceforge.pinyin4j.PinyinHelper.toHanyuPinyinStringArray(cl_chars[i], defaultFormat)[0];
                else
                    hanyupinyin = hanyupinyin + cl_chars[i];
        }
        catch (BadHanyuPinyinOutputFormatCombination e)
        {
            System.out.println("字符不能转成汉语拼音");
        }
        return hanyupinyin;
    }
}
